/**
 * JAVACC DEMO 1.0
 * @copy right zbxsoft company All rights reserved. 
 * @Package com.zbxsoft.platform.template.helper  
 */
package com.apache.info.template.helper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.apache.api.vo.ParamsVo;
import com.apache.info.entity.PubMetadata;
import com.apache.info.entity.PubSortInfo;
import com.apache.info.manager.PubMetadataManager;
import com.apache.info.manager.PubSortInfoManager;
import com.apache.info.template.FreemarkerHelper;
import com.apache.info.util.FileOperate;

/**
 * description:  生成配置实现类
 * @author dyhou 创建时间：2016-9-30  
 * @Copyright(c)2014:北京中百信软件技术有限公司
 */
public class ConfigTempManager extends SupperTemplateManager {

	public ConfigTempManager(PubSortInfoManager sortInfoManager, PubMetadataManager metadataManager) {
		this.init(sortInfoManager, metadataManager);
	}

	@SuppressWarnings("unchecked")
	public boolean createConfig(Map<String, Object> param) {
		boolean mark = false;
		List<PubSortInfo> list = (List<PubSortInfo>) param.get("sortList");
		String jarPath = String.valueOf(param.get("packPath"));
		String projectName = String.valueOf(param.get("projectName"));
		String xmlPath = String.valueOf(param.get("filePath"));
		//		String sysName = jarPath.substring(jarPath.lastIndexOf(".") + 1);//com.zbxsoft.utr中的utr
		String sysName = String.valueOf(param.get("sysCode"));
		try {
			createSpring(list, jarPath, projectName, xmlPath, sysName);
			createHibernateAndMyibatis(list, jarPath, projectName, xmlPath, sysName);
			mark = true;
		} catch (Exception e) {
			log.error("生成配置失败", e);
		}
		return mark;
	}

	private void createSpring(List<PubSortInfo> list, String jarPath, String projectName, String filePath,
			String sysName) throws Exception {
		String xmlPath = filePath + "/" + projectName + "/src/main/resources/spring/";
		FileOperate.getInstance().newCreateFolder(xmlPath);
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("packPath", jarPath);
		data.put("reqName", sysName);
		data.put("sysName", FileOperate.getInstance().toUpperFirst(sysName));
		data.put("sorts", list);
		String result = FreemarkerHelper.instance().getTemplateStr("config/spring/applicationContext.inc", data);
		FileOperate.getInstance().newCreateFile(xmlPath + "applicationContext-" + sysName + ".xml", result);
	}

	@SuppressWarnings("rawtypes")
	private void createHibernateAndMyibatis(List<PubSortInfo> list, String jarPath, String projectName,
			String filePath, String sysName) throws Exception {
		String xmlPath = filePath + "/" + projectName + "/src/main/resources/";
		//		FileOperate.getInstance().newCreateFolder(xmlPath + "hibernate/config/");
		FileOperate.getInstance().newCreateFolder(xmlPath + "ibatis/sqlmaps/");
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("packPath", jarPath);
		ParamsVo vo = new ParamsVo();
		PubMetadata metadata = new PubMetadata();
		for (PubSortInfo map : list) {
			metadata.setSortId(String.valueOf(map.getSortId()));
			vo.setObj(metadata);
			List mts = (List) metadataManager.getList(vo);
			data.put("metadatas", mts);
			data.put("sort", map);
			String result = FreemarkerHelper.instance().getTemplateStr("config/ibatis/ObjNameMapper.inc", data);
			result = result.replaceAll("###", "#{");
			FileOperate.getInstance().newCreateFile(xmlPath + "ibatis/sqlmaps/" + map.getObjName() + "Mapper.xml",
					result);
			//			result = FreemarkerHelper.instance().getTemplateStr("config/hibernate/ObjName.inc", data);
			//			FileOperate.getInstance().newCreateFile(
			//					xmlPath + "hibernate/config/" + String.valueOf(map.get("objName")) + ".hbm.xml", result);
		}
	}

}
